package data;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;

public class POSOrder extends AbstractData {

	private long id = -1;
	private long displayId;
	private long hotelId;
	private List<POSorderItem> item;
	private String itemString;
	private boolean status;
	
	public POSOrder()
	{
		status = true;
	}
	
	public POSOrder(long id){
		this.id = id;
		this.status = true;
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}


	public long getDisplayId() {
		return displayId;
	}
	public void setDisplayId(long displayId) {
		this.displayId = displayId;
	}
	public long getHotelId() {
		return hotelId;
	}

	public void setHotelId(long hotelId) {
		this.hotelId = hotelId;
	}
	public List<POSorderItem> getItem() {
		if ((item == null||item.size()== 0) && !(itemString == null || itemString.isEmpty())) {
			String[] idOfList = SplitList(itemString);
			List<POSorderItem> list = new ArrayList<POSorderItem>();
			if(idOfList.length == 1 && idOfList[0].equals("")){
				item = null;
			}else{
				for (String id : idOfList) {
					POSorderItem a = new POSorderItem(Long.parseLong(id));
					list.add(a);
				}
				for(POSorderItem a : list)
					a.LoadData();
				item = list;			
			}
		}
		return item;
	}
	
	public List<POSorderItem> getReloadItem() {
		item = null;
		return getItem();
	}

	public void setItem(List<POSorderItem> item) {
		this.itemString = CombineList(item);
		this.item = item;
	}

	public String getItemString() {
		return itemString;
	}

	public void setItemString(String itemString) {
		this.itemString = itemString;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	@Override
	public Entity getEntity() {
		Entity item;
		if(this.key == null)
			item = new Entity(EntityName.POSOrder);
		else
			item = new Entity(this.key);

		item.setProperty("displayId", this.displayId);
		item.setProperty("hotelId", this.hotelId);
		item.setProperty("status", this.status);
		item.setProperty("item", this.itemString);
		return item;
	}
	@Override
	public void setEntity(Entity entity) {
		this.id = entity.getKey().getId();
		this.displayId = (Long) entity.getProperty("displayId");
		this.hotelId = (Long) entity.getProperty("hotelId");
		this.itemString = (String) entity.getProperty("item");
		this.status = (Boolean) entity.getProperty("status");
		this.key = entity.getKey();
	}
	
	public void LoadData() {
		if (this.id > -1) {
			Entity entity = LoadData(EntityName.POSOrder, this.id);
			setEntity(entity);
		}
	}
}
